/*
* @Author: Marte
* @Date:   2018-11-27 16:44:41
* @Last Modified by:   Marte
* @Last Modified time: 2018-12-19 11:42:23
*/
// 下一个兄弟节点
function nextElement(obj){
    var nextNode=obj.nextElementSibling || obj.nextSibling;
    while(nextNode!=null){
        if(nextNode.nodeType!=1){
            nextNode=nextElement(nextNode);
        }else{
            break;
        }
    }
    return nextNode;
}
// 上一个元素节点
function prevElement(obj){
    var prev=obj.previousElementSibling || obj.previousSibling;
    while(prev!=null){
        if(prev.nodeType!=1){
            prev=prev.previousElementSibling || prev.previousSibling;
        }else{
            break;
        }
    }
    return prev;
}
// 子元素节点
function childElement(obj){
    var child=obj.children || obj.childNodes;
    var i=0;
    var arr=[];
    while(i<child.length){
        if(child[i].nodeType==1){
            arr.push(child[i]);
        }
        i++;
    }
    return arr;
}

// 获取第一给子节点
function firstElement(obj){
    var firstNode=obj.firstElementChild || obj.firstChild;
    while(firstNode!=null){
        if(firstNode.nodeType!=1){
            firstNode=nextElement(firstNode);
        }else{
            break;
        }

    }
    return firstNode;
}
// 最后一个元素节点
function lastElement(obj){
    var lastNode=obj.lastElementChild || obj.lastChild;
    while(lastNode!=null){
        if(lastNode.nodeType!=1){
            lastNode=prevElement(lastNode);
        }else{
            break;
        }
    }
    return lastNode;
}

// 获取样式表中的样式
function getStyle(elem){
    if(elem.currentStyle){
        return elem.currentStyle;
    }else{
        return window.getComputedStyle(elem,null);
    }
}

// getElementsByClassName的兼容方法
function getbyClass(name,elem){
    if (document.getElementsByClassName) {
        return (elem||document).getElementsByClassName(name);
    }
    var ele = (elem||document).getElementsByTagName("*");
    var newArr = [];
    for (var i = 0; i < ele.length; i++) {
        var className = ele[i].className;
        var classN = className.split(" ");
        for (var j = 0; j < classN.length; j++) {
            if (classN[j]==name) {
               newArr.push(ele[i]);
               break;
            };
        };

    };
    return newArr;
}



// 添加事件
function addEvent(elem,eventType,fun){
    if(elem.addEventListener){
        console.log("listener")
        elem.addEventListener(eventType,fun);
    }else if(elem.attachEvent){
        console.log("attach")
        elem.attachEvent('on'+eventType,function(){
            fun.call(elem);//修改this指向为elem.
        });

    }else{
        console.log("on");
        elem['on'+eventType]=fun;
    }
}

// 解除事件
function removeEvent(elem,eventType,funName){
    if(elem.removeEventListener){
        elem.removeEventListener(eventType,funName);
    }else if(elem.detachEvent){
        elem.detachEvent('on'+eventType,funName);
    }else{
        elem['on'+eventType]=false;
    }
}

// 阻止冒泡
function stopBubble(e){
    if(e.stopPropagation){
        return e.stopPropagation();
    }else{
        return e.cancelBubble=true;
    }
}

// 滚动条卷进去的距离
function getScroll(){
    if(window.pageXOffset){
        return {
            x:window.pageXOffset,
            y:window.pageYOffset
        }
    }else{
        return {
            x:document.body.scrollLeft+document.documentElement.scrollLeft,
            y:document.body.scrollTop+document.documentElement.scrollTop
        }
    }
}
// 获取方法： getScroll().x
//
// 获取元素在页面中的位置---只使用与存在文档流中的元素
 function getElemPos(elem){
    if(elem.offsetParent.offsetParent==null){
        return {
            top:elem.offsetTop,
            left:elem.offsetLeft
        }
    }else{
        return {
            top:elem.getBoundingClientRect().top+getScroll().y,
            left:elem.getBoundingClientRect().left+getScroll().x
        }
    }
}